'use strict';
//你要把这个公共模块依赖给加载进来，右键选择管理模块公共依赖
const {verifyToken} = require("wx-common");
exports.main = async (event, context) => {
	//解构
	const {userInfo,token} = event;
	
	//解密token,也就是把加密过的openid给解出来
	const payload = verifyToken(token);
	
	//连接数据库
	const db = uniCloud.database();
	const dbCmd = db.command;
	
	//对一个数据项进行更新
	const dbRes = await db.collection("users").where({
		//它是根据openid来进行增删改查的
		// openid: payload.openid
		openid:dbCmd.eq(payload.openid)
	}).update({
		nickName:userInfo.nickName, //用户名
		avatarUrl:userInfo.avatarUrl, //图片
		gender: userInfo.gender, //性别
		country: userInfo.country, //国家
		province: userInfo.province ,//省份
		city:userInfo.city //城市
	})
	
	//更新之后再查一遍返回给前端
	let getdbRes = await db.collection("users").where({
		openid:dbCmd.eq(payload.openid)
	}).get();
	return getdbRes
};
